using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace SmartContentUI.Document
{
    public partial class AddDocumentProp : UserControl
    {
        #region Variables
        private SmartContentBusiness.Session session;
        private int tableId;
        private System.Collections.ArrayList userRights;
        private bool clicked;
        #endregion

        #region Constructor
        public AddDocumentProp(SmartContentBusiness.Session ses, int tableId)
        {
            InitializeComponent();
            this.session = ses;
            this.tableId = tableId;
            this.userRights = new System.Collections.ArrayList();
            clicked = false;
        }
        #endregion

        #region Public Methods
        public void fillUsers()
        {
            SmartContentDB.Interfaces.User[] users = this.session.getUserList();
            if (users != null)
            {
                this.user_lbox.Items.Clear();
                this.user_lbox.Items.AddRange(users);
            }
        }
        public SmartContentDB.Interfaces.UserRight[] getUserRights()
        {
            clearEmptyRights();
            if (this.userRights.Count == 0)
                return null;
            SmartContentDB.Interfaces.UserRight[] rights = new SmartContentDB.Interfaces.UserRight[this.userRights.Count];
            this.userRights.CopyTo(rights);
            return rights;
        }
        public void clearForm()
        {
            this.userRights.Clear();
            this.viewItem.Checked = false;
            this.editItem.Checked = false;
            this.xprtItem.Checked = false;
            this.prntItem.Checked = false;
        }
        public void loadRightsForRegistry(int contentId, int registryId)
        {
            SmartContentDB.Interfaces.User[] users = this.session.getUserList();
            if (users != null)
            {
                for (int i = 0; i < users.Length; i++)
                {
                    SmartContentDB.Interfaces.UserRight right = this.session.getUserRights(users[i].getId(), contentId, registryId);
                    this.userRights.Add(right);
                }
            }
        }
        #endregion

        #region Private Methods

        private void clearEmptyRights()
        {
            ArrayList newUserRightsColl = new ArrayList();
            foreach(SmartContentDB.Interfaces.UserRight tmp in userRights)
            {
                if (!tmp.View && !tmp.Edit &&
                    !tmp.Export && !tmp.Print)
                {
                    //do nothing
                }
                else
                {
                    newUserRightsColl.Add(tmp);
                }
            }
            userRights = newUserRightsColl;
        }
        private void updateUserRight(int user)
        {
            bool finded = false;
            for (int i = 0; i < userRights.Count; i++)
            {
                if (((SmartContentDB.Interfaces.UserRight)userRights[i]).UserId == user)
                {
                    ((SmartContentDB.Interfaces.UserRight)userRights[i]).View = this.viewItem.Checked;
                    ((SmartContentDB.Interfaces.UserRight)userRights[i]).Edit= this.editItem.Checked;
                    ((SmartContentDB.Interfaces.UserRight)userRights[i]).Export = this.xprtItem.Checked;
                    ((SmartContentDB.Interfaces.UserRight)userRights[i]).Print = this.prntItem.Checked;
                    finded = true;
                    break;
                }
            }
            if (!finded)
            {
                SmartContentDB.Interfaces.UserRight right = new SmartContentDB.Interfaces.UserRight();
                right.UserId = user;
                right.ContainerId = this.tableId;
                right.View = this.viewItem.Checked;
                right.Edit = this.editItem.Checked;
                right.Export = this.xprtItem.Checked;
                right.Print = this.prntItem.Checked;
                this.userRights.Add(right);
            }
        }

        private void setUserRigthUI(int user)
        {
            bool finded = false;
            for (int i = 0; i < userRights.Count; i++)
            {
                if (((SmartContentDB.Interfaces.UserRight)userRights[i]).UserId == user)
                {
                    this.viewItem.Checked = ((SmartContentDB.Interfaces.UserRight)userRights[i]).View;
                    this.editItem.Checked = ((SmartContentDB.Interfaces.UserRight)userRights[i]).Edit;
                    this.xprtItem.Checked = ((SmartContentDB.Interfaces.UserRight)userRights[i]).Export;
                    this.prntItem.Checked = ((SmartContentDB.Interfaces.UserRight)userRights[i]).Print;
                    finded = true;
                    break;
                }
            }
            if (!finded)
            {
                this.viewItem.Checked = false;
                this.editItem.Checked = false;
                this.xprtItem.Checked = false;
                this.prntItem.Checked = false;
            }
        }
        #endregion

        #region Events
        private void user_lbox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.user_lbox.SelectedItem != null)
                setUserRigthUI(((SmartContentDB.Interfaces.User)this.user_lbox.SelectedItem).getId());
        }
       
        private void listView1_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            if (this.user_lbox.SelectedIndices.Count == 0)
            {
                if(((ListView)sender).FocusedItem.Checked)
                    MessageBox.Show("Debe seleccionar un usuario", "Derechos", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ((ListView)sender).FocusedItem.Checked = false;
                return;
            }
            if(clicked)
                updateUserRight(((SmartContentDB.Interfaces.User)this.user_lbox.SelectedItem).getId());
            clicked = false;
        }

        private void listView1_MouseDown(object sender, MouseEventArgs e)
        {
            clicked = true;
        }
        #endregion

    }
}
